package com.xzzz.irda.guardcat.server.metric.snapshot;

import com.xzzz.common.base.exception.XzException500;
import com.xzzz.common.base.util.okhttp.HttpUtil;
import com.xzzz.irda.guardcat.core.constants.GuardcatConstant;
import com.xzzz.irda.guardcat.server.app.AppManager;
import com.xzzz.irda.guardcat.server.app.pojo.MachineInfo;
import com.xzzz.irda.guardcat.server.metric.MetricRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author xzzz
 */
@Slf4j
@Component
public class SnapshotRepository extends MetricRepository<SnapshotInfo> {

    @Autowired
    private AppManager appManager;

    public static final String OBJECT_STAT = "objectStat";
    public static final String THREAD_LIST = "threadList";
    public static final String THREAD_DUMP = "threadDump";
    public static final String DEAD_LOCK = "deadLock";

    /**
     * 对象统计信息
     *
     * @param appName   应用名称
     * @param machineId 机器名称
     * @return 响应体
     */
    public String objectStat(String appName, String machineId) {
        log.info("[GUARDCAT] 拉取对象统计快照 [{}.{}]", appName, machineId);
        MachineInfo machineInfo = getMachine(appName, machineId);
        if (machineInfo.isNotProd()) {
            log.info("非生产机器, 无法拉取数据");
            return "非生产机器, 无法拉取数据";
        }
        String url = buildUrl(machineInfo, GuardcatConstant.API.METRIC_OBJECT_STAT);
        return HttpUtil.get(url);
    }

    /**
     * 线程列表
     *
     * @param appName   应用名称
     * @param machineId 机器名称
     * @return 响应体
     */
    public String threadList(String appName, String machineId) {
        log.info("[GUARDCAT] 拉取线程列表快照 [{}.{}]", appName, machineId);
        MachineInfo machineInfo = getMachine(appName, machineId);
        if (machineInfo.isNotProd()) {
            log.info("非生产机器, 无法拉取数据");
            return "非生产机器, 无法拉取数据";
        }
        String url = buildUrl(machineInfo, GuardcatConstant.API.METRIC_THREAD_LIST);
        return HttpUtil.get(url);
    }

    /**
     * 线程堆栈信息
     *
     * @param appName   应用名称
     * @param machineId 机器名称
     * @return 响应体
     */
    public String threadDump(String appName, String machineId) {
        log.info("[GUARDCAT] 拉取线程堆栈快照 [{}.{}]", appName, machineId);
        MachineInfo machineInfo = getMachine(appName, machineId);
        if (machineInfo.isNotProd()) {
            log.info("非生产机器, 无法拉取数据");
            return "非生产机器, 无法拉取数据";
        }
        String url = buildUrl(machineInfo, GuardcatConstant.API.METRIC_THREAD_DUMP);
        return HttpUtil.get(url);
    }

    /**
     * 死锁信息
     *
     * @param appName   应用名称
     * @param machineId 机器名称
     * @return 响应体
     */
    public String deadLock(String appName, String machineId) {
        log.info("[GUARDCAT] 拉取死锁信息快照 [{}.{}]", appName, machineId);
        MachineInfo machineInfo = getMachine(appName, machineId);
        if (machineInfo.isNotProd()) {
            log.info("非生产机器, 无法拉取数据");
            return "非生产机器, 无法拉取数据";
        }
        String url = buildUrl(machineInfo, GuardcatConstant.API.METRIC_DEAD_LOCK);
        return HttpUtil.get(url);
    }

    public MachineInfo getMachine(String appName, String machineId) {
        MachineInfo machine = appManager.getMachine(appName, machineId);
        if (machine == null) {
            throw new XzException500(String.format("机器不存在 [%s.%s]", appName, machineId));
        }
        return machine;
    }

    @Override
    public void save(MachineInfo machineInfo, SnapshotInfo snapshotInfo) {

    }

    @Override
    public void expire() {

    }

    @Override
    public SnapshotInfo getByMachineId(String machineId) {
        return null;
    }

    @Override
    public List<SnapshotInfo> listByMachineId(String machineId) {
        return null;
    }
}
